package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCommentReply;
import cn.wolfcode.trip.base.domain.TravelReply;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentReplyMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.TravelReplyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentReplyService;
import cn.wolfcode.trip.base.service.ITravelReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 攻略点评的实现类
 */
@Service
public class TravelReplyServiceImpl implements ITravelReplyService {
    @Autowired
    private TravelReplyMapper travelReplyMapper;

    @Autowired
    private TravelMapper travelMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelReply> list = travelReplyMapper.selectForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void saveReply(TravelReply entity) {
        //设置评论时间
        entity.setCreateTime(new Date());
        //设置游记的作者
        Long travelId = entity.getTravelId();
        User author = travelMapper.selectByPrimaryKey(travelId).getAuthor();
        entity.setAuthor(author);
        //保存
        travelReplyMapper.insert(entity);
    }

    @Override
    public int getCountByTravelId(Long travelId) {
        return travelReplyMapper.getCountByTravelId(travelId);
    }

}
